package net.starwifi.duobao.web;




import net.starwifi.duobao.entity.Operator;
import net.starwifi.duobao.service.RSAService;
import net.starwifi.duobao.utils.WebUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.security.interfaces.RSAPublicKey;
import java.util.*;

/**
 * Controller - LoginController.
 *
 * @author ruoo
 * @version 1.0
 */
@Controller("adminLoginController")
@RequestMapping("/login")
public class LoginController extends BaseController {


    @Autowired
    private RSAService rsaService;

    /**
     * 登录页面
     */
    @RequestMapping(method = RequestMethod.GET)
    public String loginGet(String redirectUrl, HttpServletRequest request, ModelMap model) {
        String tag=WebUtils.getCookie(request, "tag");
        model.addAttribute("redirectUrl", redirectUrl);
        model.addAttribute("captchaId", UUID.randomUUID().toString());
        RSAPublicKey publicKey = rsaService.generateKey(request);
        model.addAttribute("modulus", Base64.encodeBase64String(publicKey.getModulus().toByteArray()));
        model.addAttribute("exponent", Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray()));
        if(tag!=null&&tag.equals("fqg")){
            return "/login/fqg";
        }
        return "/login/index";
    }

    /**
     * 登录POST
     */
    @RequestMapping(method = RequestMethod.POST)
    public  String  loginPost(HttpServletRequest request,String redirectUrl,Model model,RedirectAttributes redirectAttributes) {
        String loginFailure = (String) request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
        String msg=null;
        String tag=WebUtils.getCookie(request, "tag");
        if (loginFailure != null) {
            if (loginFailure.equals("org.apache.shiro.authc.pam.UnsupportedTokenException")) {
                msg="验证码错误";
            } else if (loginFailure.equals("org.apache.shiro.authc.UnknownAccountException")) {
                msg="用户不存在";
            } else if (loginFailure.equals("org.apache.shiro.authc.DisabledAccountException")) {
                msg="账户失效";
            } else if (loginFailure.equals("org.apache.shiro.authc.LockedAccountException")) {
                msg="账户被锁定";
            } else if (loginFailure.equals("org.apache.shiro.authc.IncorrectCredentialsException")) {
                msg="用户名或者密码错误";
            } else if (loginFailure.equals("org.apache.shiro.authc.AuthenticationException")) {
                msg="登录失败";
            }else{
                msg="登录失败";
            }
        }
        model.addAttribute("redirectUrl", redirectUrl);
        model.addAttribute("captchaId", UUID.randomUUID().toString());
        RSAPublicKey publicKey = rsaService.generateKey(request);
        model.addAttribute("msg",msg);
        model.addAttribute("modulus", Base64.encodeBase64String(publicKey.getModulus().toByteArray()));
        model.addAttribute("exponent", Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray()));
        if(tag!=null&&tag.equals("fqg")){
            return "/login/fqg";
        }
        return "/login/index";
    }

    /**
     * 注销
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String execute(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        session.removeAttribute(Operator.PRINCIPAL_ATTRIBUTE_NAME);
        return "redirect:/";
    }
}
